<?php
N::loadClass('N_Controller_Interface');
/**
 * 基础控制器类
 *
 */
class N_Controller_Action implements N_Controller_Interface
{
	protected $_dispather;
	
	public function __construct()
	{
		$dipatcherClass = N::getIni('DISPATCHER_CLASS_NAME');
		$this->_dispather = N::getSingleton($dipatcherClass);
	}
	
	public function actionIndex()
	{
		//子类的覆盖该方法以显示自己的页面
	}
	
	/**
	 * 获得Model对象
	 *
	 * @return N_Model_Abstract Object
	 */
	protected function _getModel()
	{
		$conClass = get_class($this);
		$conPrefix = N::getIni('CONTROLLER_PREFIX');
		$modelPrefix = N::getIni('MODEL_PREFIX');
		$modelClass = str_replace($conPrefix, $modelPrefix, $conClass);
		//如果有对应的model派生类则返回model派生类,否则返回model基类
		//model派生的命名规则为 APP_Model_Test(按照命名规则,该派生类应该存放在 入口文件目录/app/model/test.php中)
		//其中APP是相对入口文件的文件夹名
		try 
		{
			$model = N::getSingleton($modelClass);
			return $model;
		}
		catch (Exception $e)
		{
			$modelClass = N::getIni('MODEL');
			$model = N::getSingleton($modelClass);
			return $model;
		}
	}
	
	/**
	 * 获得视图对象
	 *
	 * @return View Object
	 */
	protected function _getView()
	{
		$conClass = __CLASS__;
		$conPrefix = N::getIni('CONTROLLER_PREFIX');
		$viewPrefix = N::getIni('VIEW_PREFIX');
		$viewClass = str_replace($conPrefix, $viewPrefix, $conClass);
		//类似于_getModel方法
		try 
		{
			N::loadClass($viewClass);
			$view = N::getSingleton($viewClass);
			return $view;
		}
		catch (Exception $e)
		{
			$viewClass = N::getIni('VIEW');
			$view = N::getSingleton($viewClass);
			return $view;
		}
	}
	
	/**
	 * 转发请求
	 *
	 * @param string $controller
	 * @param string $action
	 */
	protected function render(string $controller, string $action = null)
	{
		$this->_dispather->setController($controller);
		$this->_dispather->setAction($action);
		$this->_dispather->dispatching();
	}
}